package com.example.controller;

import com.example.service.OrderService;
import com.example.util.Filter;
import com.example.util.QueryParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RequestMapping("/order")
@RestController
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 获取报单记录
     * @param partnerId
     * @param page
     * @param size
     * @return
     */
    @PostMapping("/orderRecord")
    public Map orderRecord(@RequestParam("partnerId") Integer partnerId, @RequestParam(name = "page", defaultValue = "0", required = true) Integer page,
                           @RequestParam(name = "size", defaultValue = "10", required = true) Integer size) {
        Map<String, Object> map = new HashMap<>();
        QueryParams queryParams = new QueryParams();
        queryParams.clearAll().and(Filter.eq("t2.`store_id`", partnerId)).orderDESC("t1.create_time");
        PageRequest pageRequest = new PageRequest(page, size);
        try {
            map = orderService.getOrderRecord(queryParams,pageRequest);
            map.put("code", "1");
        }catch (Exception e){
            map.put("code", "0");
            map.put("msg","数据获取异常");
        }
        return map;
    }

    /**
     * 评房历史记录
     * @param partnerId
     * @param page
     * @param size
     * @return
     */
    @PostMapping("/assessmentOfReal")
    public Map assessmentOfReal(@RequestParam("partnerId") Integer partnerId, @RequestParam(name = "page", defaultValue = "0", required = true) Integer page,
                                @RequestParam(name = "size", defaultValue = "10", required = true) Integer size){
        Map<String,Object> map =new HashMap<>();
        QueryParams queryParams = new QueryParams();
        queryParams.clearAll().and(Filter.eq("t1.`store_id`", partnerId)).orderDESC("t2.input_time");
        PageRequest pageRequest = new PageRequest(page, size);
        try {
            map = orderService.assessmentOfReal(queryParams,pageRequest);
            map.put("code", "1");
        }catch (Exception e){
            map.put("code", "0");
            map.put("msg","数据获取异常");
        }
        return map;
    }


    /**
     * 获取佣金及奖励明细
     * @param partnerId
     * @param page
     * @param size
     * @return
     */
    @PostMapping("/commissionInfo")
    public Map commissionInfo(@RequestParam("partnerId") Integer partnerId, @RequestParam(name = "page", defaultValue = "0") Integer page,
                              @RequestParam(name = "size", defaultValue = "10") Integer size){
        Map<String,Object> map =new HashMap<>();
        QueryParams queryParams = new QueryParams();
        queryParams.clearAll().and(Filter.eq("partner_id", partnerId)).orderDESC("actual_lend_time");
        PageRequest pageRequest = new PageRequest(page, size);
        try {
            map = orderService.commissionInfo(queryParams,pageRequest);
            map.put("code", "1");
        }catch (Exception e){
            map.put("code", "0");
            map.put("msg","数据获取异常");
        }
        return map;
    }



    /**
     * 获取佣金及奖励汇总
     * @param partnerId
     * @return
     */
    @PostMapping("/commissionSummary")
    public Map commissionSummary(@RequestParam("partnerId") Integer partnerId){
        Map<String,Object> map =new HashMap<>();
        try {
            map = orderService.commissionSummary(partnerId);
            map.put("code", "1");
        }catch (Exception e){
            map.put("code", "0");
            map.put("msg","数据获取异常");
        }
        return map;
    }
}
